*******************
* Constructing HH outcomes for Global South COVID paper
* Refugees Sample
* 2020-06-23
* last updated: 2020-09-09
* Magdalena Larreboure
*******************

set more off
gl weight weight


use "$dr/KEN2_raw.dta", clear

*number of HH members
rename current_num_adults n_adults
rename hhsize n_hhmem
label var n_adults "Number of adults in the household"
label var n_hhmem "Number of household members, including children"


********************************************************************************************************
							******************* Food security *******************		
********************************************************************************************************

// - Missed meals
// - Reduced portions


loc fdays s5_q39a_hungryadult s5_q39b_hungrychild s5_q40a_skippedadult s5_q40b_skippedchild s5_q41a_nofoodadult s5_q41b_nofoodchild //how many days out of 7 qs42-44 in paper version
foreach var of varlist `fdays' {
loc name_`var'=substr("`var'", 9, 12)
g f1_d_`name_`var'' =`var'
replace f1_d_`name_`var''=. if `var'==-99

}
label var f1_d_hungryadult "Number of days adults have gone to bed hungry in the past 7 days"
label var f1_d_hungrychild "Number of days children have gone to bed hungry in the past 7 days" //42b. In the past 7 days, how many days have CHILDREN in your household gone to bed hungry?"
label var f1_d_skippedadult "Number of days adults have skipped meals in the past 7 days" //43a. In the past 7 days, how many days have ADULTS in your household skipped meals or cut the amount of meals?
label var f1_d_skippedchild "Number of days children have skipped meals in the past 7 days"
label var f1_d_nofoodadult "Number of days adults have gone the entire day without food" //44a. In the past 7 days, how many days have ADULTS in your household gone entire days without food?
label var f1_d_nofoodchild "Number of days children have gone the entire day without food"


g missedmeals=1 if (f1_d_skippedadult>=1 & f1_d_skippedadult<=7) | (f1_d_skippedchild>=1 & f1_d_skippedchild<=7)
replace missedmeals=1 if (f1_d_nofoodadult>=1 & f1_d_nofoodadult<=7) | (f1_d_nofoodchild>=1 & f1_d_nofoodchild<=7)
replace missedmeals=0 if f1_d_skippedadult==0 & f1_d_skippedchild==0 & f1_d_nofoodadult==0 & f1_d_nofoodchild==0
label var missedmeals "Share of sample missing or cutting meals in the past 7 days"

********************************************************************************************************
							******************* Income *******************		
********************************************************************************************************

// - Drop in income
// I1: Agricultural earnings NOW: 4.10

// I1: Agricultural earnings: 4.10
g i1=s4_q10_agearnings if !inlist(s4_q10_agearnings,-99, -98, 99)
replace i1=i1/$pppfactor
// replace i1=i1/n_hhmem
replace i1=0 if s4_q1_agactivity_0==1
label var i1 "Agricultural earnings in the past 14 days"


// I1: Agricultural earnings FEBRUARY: 4.10a
** For ag, this is last year **
g i1_pre=s4_q10a_agearningstrend if !inlist(s4_q10_agearnings,-99, -98, 99)
replace i1_pre=i1_pre/$pppfactor
// replace i1=i1/n_hhmem
replace i1_pre=0 if s4_q1_agactivity_0==1
label var i1_pre "Agricultural earnings in at the same time last year"


// I2: Self-employment earnings: sum of 4.20 across enterprises -  Over the past 14 days, what was the total profit of this enterprise?
gen selfemp = s4_q12_num_ent > 0 if s4_q12_num_ent >= 0

foreach var of varlist s4_q20_profits_* {
replace `var'=. if inlist(`var',-99,-98,99)
}

forval i=1/2 {
replace s4_q20_profits_`i'=0 if s4_q15_operating_`i'!=1
}
egen i2=rowtotal(s4_q20_profits_*) , m
forval i = 1/2 {
	replace i2 = . if s4_q20_profits_`i' == .
}
sum i2 if selfemp == 1, d
replace i2=i2/$pppfactor
// replace i2=i2/n_hhmem
label var i2 "Self-employment earnings in the past 14 days"


// I2: Self-employment earnings FEBRUARY: sum of 4.21a across enterprises -  Over the past 14 days, what was the total profit of this enterprise?
foreach var of varlist s4_q21a_profits_* {
replace `var'=0 if `var' == .
replace `var'=. if inlist(`var',-99,-98,99)
}


egen i2_pre=rowtotal(s4_q21a_profits_*), m
foreach var of varlist s4_q21a_profits_* {
	replace i2_pre = . if `var' == .
}
replace i2_pre=i2_pre/$pppfactor
// replace i2=i2/n_hhmem
label var i2_pre "Self-employment earnings in the a typical 14 day period in February"


// I3: Wage earnings NOW: 4.28

loc wagehhmemb s4_q28_cashamt_*
foreach var of varlist `wagehhmemb' {
replace `var'=. if `var'==-99 | `var'==-98  |`var'==99 | `var'==98
replace `var'=0 if s4_q23_whoemployed__98==1
}
egen i3=rowtotal(`wagehhmemb') , m
foreach var of varlist `wagehhmemb' {
	replace i3 = . if `var' == .
}
replace i3=i3/$pppfactor
// replace i3=i3/n_hhmem
label var i3 "Wage earnings in the past 14 days"

// I3: Wage earnings FEBRUARY: 4.28a
loc wagefeb s4_q28a_cashamt_*
foreach var of varlist `wagefeb' {
replace `var'=0 if s4_q23_whoemployed__98==1
replace `var'=. if `var'==-99 | `var'==-98 | `var'==99 | `var'==98
}
egen i3_pre=rowtotal(`wagefeb') , m
foreach var of varlist `wagefeb' {
	replace i3_pre = . if `var' == .
}

replace i3_pre=i3_pre/$pppfactor
// replace i3=i3/n_hhmem
label var i3_pre "Biweekly wage earnings back in February"


// I4: Total Household earnings NOW
loc i i1 i2 i3
egen i4=rowtotal(`i') , m
label var i4 "Total household earnings in the past 14 days"


// I4: Total pre-period household earnings
loc i i1_pre i2_pre i3_pre
egen i4_pre =rowtotal(`i') , m
forval i = 1/3 {
	replace i4_pre = . if i`i'_pre == .
}
label var i4_pre  "Total household earnings in the pre-period (February for non-ag, last year for ag)"


gen inc3_total_drop = (i4_pre > i4) & i4 != . & i4_pre != .

gen inc3_total_increase = (i4_pre < i4) & i4 != . & i4_pre != .



** add in layoffs that weren't replaced **
egen nemployed_now= rowtotal(s4_q23_whoemployed_*) , m // 24. Which ADULT household members are currently employed, working for pay?
replace nemployed_now=0 if s4_q23_whoemployed__98==1

egen nlaidoff_smarch=rowtotal(s4_q33_wholaidoff_*) , m // 29. Since January 2020, which ADULTS in your household were laid off / lost their job?
replace nlaidoff_smarch=0 if s4_q33_wholaidoff__98==1
replace nlaidoff_smarch=0 if inlist(s4_q33b_whenlaidoff_1,6,7,8) | inlist(s4_q33b_whenlaidoff_2,6,7,8) | inlist(s4_q33b_whenlaidoff_3,6,7,8) // laid off in February or before

g nemployed_march=nemployed_now+nlaidoff_smarch
replace nemployed_march=. if nemployed_now==. | nlaidoff_smarch==.

g lostjobs= cond(nemployed_march>nemployed_now, 1, 0)
replace lostjobs=. if nemployed_march==. | nemployed_now==.

// forval j = 1/5 {
// 	forval i = 1/3 {
// 		replace lostjob`j' = 0 if s4_q33_wholaidoff_`j' == 1 & inlist(s4_q33b_whenlaidoff_`i',6,7,8) // don't count Jan/Feb layoffs
// }
// }

** TODO
// egen lostjobs = rowtotal(lostjob?), m

forval i = 1/3 {
	gen gainjob`i' = s4_q27a_workbeforemarch_`i' == 1
}
egen gainjobs = rowtotal(gainjob?), m

replace inc3_total_drop = 1 if lostjobs > 0 & gainjobs == 0 & lostjobs != .
replace inc3_total_increase = 1 if lostjobs == 0 & gainjobs > 0 & gainjobs != .

tab inc3_total_drop
tab inc3_total_drop if i4_pre > 0 | lostjobs > 0

*Adding obs for: closed an enterprise (and didn't re-open / start one).

g startenterprise=1 if s4_q13c_startyear_1==2020 | s4_q13c_startyear_2==2020
replace startenterprise=0 if (inlist(s4_q13c_startmonth_1, 1, 2) & s4_q13c_startyear_1==2020) | (inlist(s4_q13c_startmonth_2, 1, 2) & s4_q13c_startyear_2==2020)

replace inc3_total_drop=1 if (inlist(s4_q15_operating_1, 2, 3, 4) | inlist(s4_q15_operating_2, 2, 3, 4)) & startenterprise!=1
replace inc3_total_increase=1 if !inlist(s4_q15_operating_1, 2, 3, 4) & !inlist(s4_q15_operating_2, 2, 3, 4) & startenterprise==1

********************************************************************************************************
							******************* Employment *******************		
********************************************************************************************************

// - Drop in employment

			
// 1. Share of employed that lost job since February - emp1_lostjob

g emp1_lostjob=nlaidoff_smarch/nemployed_march
label var emp1_lostjob "Share of employed that lost job since February"

*Household level employment drop:

g employmentdrop=cond(lostjobs>gainjobs,1,0) // this would be: the amount of people laid off in the hh is greater than the amount of people who gained a job since march
label var employmentdrop "Employment drop in the household"

replace employmentdrop=. if nemployed_march==0 // to make it conditional on anyone in the household working for wages in Feb 2020


********************************************************************************************************
							******************* Access to markets *******************		
********************************************************************************************************

// - Reduction in access to markets


********************************************************************************************************
							******************* NGO or GOV support *******************		
********************************************************************************************************
// - Received new NGO or Govt Support

// I5: Support from government, NGOs/community groups and politicians: 5.37a + 5.38a + 5.39a
loc support s7_q3a_value s7_q4a_value s7_q5a_value 
loc support2 s7_q3a_value s7_q4a_value 
replace s7_q3a_value=0 if s7_q3_govthelp==1
replace s7_q4a_value=0 if s7_q4_ngohelp==1
replace s7_q5a_value=0 if s7_q5_politicianhelp==1
foreach var of varlist `support' {
replace `var'=. if `var'==-99
}
g i5a=s7_q3a_value/$pppfactor
g i5b=s7_q4a_value/$pppfactor
g i5c=s7_q5a_value/$pppfactor
// replace i5a=i5a/n_hhmem
// replace i5b=i5b/n_hhmem
// replace i5c=i5c/n_hhmem
label var i5a "Support from government"
label var i5b "Support from NGOs/community groups"
label var i5c "Support from politicians"

egen i5=rowtotal(`support2') , m
replace i5=i5/$pppfactor
// replace i5=i5/n_hhmem
label var i5 "Support from government and NGOs/community groups (pc) in the past 14 days"

g anysupport=1 if i5!=0
replace anysupport=0 if i5==0
label var anysupport "Share of sample receiving any support from government and NGOs in the past 14 days"


// s7_q2b_change s7_q4b_change s7_q5b_change
********************************************************************************************************
							******************* Health care access *******************		
********************************************************************************************************
g healthaccess= s9_q8_accesstreatment==0 | inlist(s9_q6_check_fr,2,3) if s9_q8_accesstreatment!=. | s9_q6_check_fr!=.  // no access to treatment or reduced frequency for routine check-ups

label var healthaccess "Health care access delayed" // no access to treatment or reduced frequency for routine check-ups


***** FEBRUARY *******
loc feb s4_q19_laidoff_feb_1 s4_q20_salestrend_num_1 s4_q21a_profits_1 s4_q28a_cashamt_biweek_1 s7_q1b_change s7_q2b_change s7_q3b_change s7_q4b_change s7_q5b_change

loc outcomes missedmeals employmentdrop inc3_total_drop anysupport healthaccess inc3_total_increase

g female = s2_q3_gender==2

g rural = urban==1
g power = s2_q21a_powergrid


 qui sum i4_pre , detail
 g b_inc= cond(i4_pre <= `r(p50)', 1, 0) 

 preserve
keep `outcomes' b_inc weight rural female n_hhmem s2_q4_educ

  save "$da/KEN2_maintable.dta" , replace

restore

g completesecondary =1 if inlist(s2_q4_educ,6,7,8,9)
replace completesecondary=0 if inlist(s2_q4_educ,2,3,4,5,10)

g yearseduc=. if s2_q4_educ==1 // Other
replace yearseduc=0 if s2_q4_educ==2 // No Formal Education
replace yearseduc=2 if s2_q4_educ==3 // Pre-primary
replace yearseduc=8 if s2_q4_educ==4 // Primary
replace yearseduc=9 if s2_q4_educ==5 // Post-primary, vocational
replace yearseduc=12 if s2_q4_educ==6 // Secondary
replace yearseduc=15 if s2_q4_educ==7 // College, middle-level
replace yearseduc=15 if s2_q4_educ==8 // University undergraduate
replace yearseduc=15 if s2_q4_educ==9 // University postgraduate
replace yearseduc=4 if s2_q4_educ==10 // Madrassa/ Duksi

// C1: Total food expenditure in the past 7 days: 4.1.1 + 4.3.Groceries/Food
loc c1 s5_q3a_food s5_q1_consumptionamt // expenditure + own agricultural or pastoral production
replace s5_q3a_food=. if s5_q3a_food==-99
replace s5_q1_consumptionamt=. if s5_q1_consumptionamt==-99
egen c1= rowtotal(`c1') , m
replace c1=c1/$pppfactor
// replace c1=c1/n_hhmem
label var c1 "Total food expenditure in the past 7 days"


// C2: Total food consumption in the past 7 days: C1 +  4.1.2a
replace s5_q2a_foodamt=. if s5_q2a_foodamt==-99
replace s5_q2a_foodamt=0 if s5_q2_receivedfood==1
g s5_q2a_foodamt_PPP = s5_q2a_foodamt/$pppfactor 
// replace s5_q2a_foodamt_PPP = s5_q2a_foodamt_PPP/n_hhmem 
loc c2 c1 s5_q2a_foodamt_PPP 
egen c2= rowtotal(`c2') , m
label var c2 "Total food consumption (incl. gifts) in the past 7 days"

// C3: Non food expenditure in the past 7 days: 4.3.Household Items + 4.3.Assets + 4.3.Services + 4.3.Communication + 4.3.Housing + 4.3.Energy + 4.3.Transport + 4.3.Medical

* We ask for past 14 days for b-i
loc nfexp s5_q3b_personal s5_q3c_durables s5_q3d_services s5_q3e_comms s5_q3f_housing s5_q3g_utilities s5_q3h_transport s5_q3i_medical
foreach var of varlist `nfexp' {
replace `var'=. if `var'==-99
replace `var'=`var'/2 
}
egen c3=rowtotal(`nfexp') , m
replace c3=c3/$pppfactor
// replace c3=c3/n_hhmem
label var c3 "Non-food expenditure in the past 7 days"

// C4: Total consumption in the past 7 days: C2 + C3
egen c4=rowtotal(c2 c3) , m
label var c4 "Total consumption in the past 7 days"

g consumption=c4/n_adults

keep weight rural n_hhmem female s2_q4_educ yearseduc completesecondary consumption


  save "$da/KEN2_reptable.dta" , replace

